package otcpro_service

import (
	"beta/models"
	"beta/pkg/e"
	"beta/pkg/logger"
	"beta/pkg/util"
	"github.com/tidwall/gjson"
	"time"
)

type OtcProService struct {
	FullName string
	CustomerId string
	ReceiverEmail string
	ReceiverMobile string
	Operator string
	Token string
	Hosts map[string]*models.AppEnv
	traceId string
}

func (otcpro OtcProService) CreateOrdinaryOrder(productId, productVersionId, startDate, endDate string) (string, error)  {

	host := otcpro.Hosts["otcpro"].AppDomain
	url := host + "/otcpro/v1/order/ordinaryOrder/save"

	if startDate == "" || endDate == "" {
		startDate = time.Now().AddDate(0, -2, 0).Format(e.TimeDayFormat)
		endDate = time.Now().AddDate(1, -2, -1).Format(e.TimeDayFormat)
	}

	baseInfo := BaseInfo{
		AccessID:     "",
		OrderType:    "0",
		PurchaserID:  otcpro.CustomerId,
		ServiceType:  "01",
		SalesChannel: "50",
		TerminalType: "1",
		OrderMode:    "2",
	}

	var productInfoList []ProductInfo
	var ordinaryProductList []OrdinaryProduct
	
	ordinaryProductA := OrdinaryProduct{
		ProductID:          productId,
		ProductVersionID:   productVersionId,
		StandardPrice:      1,
		MinimumPrice:       1,
		SellingCount:       1,
		SellingAmount:      0,
		EffectiveDateStart: startDate,
		EffectiveDateEnd:   endDate,
		PresentTag:         "1",
		PresentQuantity:    1,
	}
	ordinaryProductList = append(ordinaryProductList, ordinaryProductA)
	benefiterId := otcpro.CustomerId

	productInfoA := ProductInfo{
		BenefiterID:         benefiterId,
		OrdinaryProductList: ordinaryProductList,
	}

	productInfoList = append(productInfoList, productInfoA)

	reductionReason := "下单减免至0"
	invoiceBeforePaid := "0"
	invoiceInfo := InvoiceInfo{
		InvoiceType:      "4",
		InvoiceTitleType: "2",
		Title:            otcpro.FullName,
	}

	deliveryInfo := DeliveryInfo{
		ReceiverEmail:  "13100010035@qq.com",
		ReceiverMobile: "13635534591",
		LocationCode:   "",
	}

	contactInfo := ContactInfo{
		ContactName: otcpro.FullName,
		Gender:      "",
		Role:        "",
		Mobile:      "",
		Phone:       "",
		Email:       "",
	}

	orderRemark := OrderRemark{
		SignerID:     e.GetOperatorId(otcpro.Operator),
		OperatorType: "2",
		OperatorID:   e.GetOperatorId(otcpro.Operator),
	}

	order := OrdinaryOrder{
		BaseInfo:          baseInfo,
		ProductInfoList:   productInfoList,
		ReductionReason:   reductionReason,
		InvoiceBeforePaid: invoiceBeforePaid,
		InvoiceInfo:       invoiceInfo,
		DeliveryInfo:      deliveryInfo,
		ContactInfo:       contactInfo,
		OrderRemark:       orderRemark,
	}

	logger.ZapLogger.Infow("【创建预付费订单】", "请求参数", order, "traceId", otcpro.traceId)

	cookie := util.GetCookie(util.BossCookieName, otcpro.Token)
	httpClient := util.NewHttpClientWithCookie(3 * time.Second, cookie).R()

	resp, respErr := httpClient.SetHeaders(e.GetHeaderMap(e.JsonHeader)).SetBody(order).Post(url)
	logger.ZapLogger.Infow("【创建预付费订单】", "请求结果", resp, "traceId", otcpro.traceId)

	if respErr != nil {
		return "", respErr
	}

	orderId := gjson.Get(resp.String(), "data.orderId").String()

	return orderId, nil
}